package com.yswl.yeeduo.ui.activity;

import java.util.Map;
import java.util.TreeMap;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.yswl.yeeduo.Constant;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.http.KAsyncTask.TaskDelegate;
import com.yswl.yeeduo.tool.KStringTool;
import com.yswl.yeeduo.ui.base.BaseActivity;
import com.yswl.yeeduo.ui.base.BaseWebViewActivity;
import com.yswl.yeeduo.ui.exception.NetworkException;
import com.yswl.yeeduo.ui.task.RegTask;
import com.yswl.yeeduo.ui.task.SendSMSCodeTask;
import com.yswl.yeeduo.ui.tool.ToastUtil;
import com.yswl.yeeduo.ui.tool.UserManger;

public class RegisterActivity extends BaseActivity implements OnClickListener, TaskDelegate {

	EditText mPhone;
	EditText mRePwd;
	EditText mCode;
	Button mSend;
	Button mConfirm;
	String userName;
	String userPwd;
	TimeCount mCount;
	TextView privTv;
	CheckBox checkB;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reg);
		setHeader("注册");
		initView();

	}

	private void initView() {
		mPhone = (EditText) findViewById(R.id.phone_et);
		mRePwd = (EditText) findViewById(R.id.pwd_edit);
		mCode = (EditText) findViewById(R.id.ca_send_code);
		mSend = (Button) findViewById(R.id.send_btn);
		mSend.setOnClickListener(this);
		mConfirm = (Button) findViewById(R.id.confirm_btn);
		mConfirm.setOnClickListener(this);

		privTv = (TextView) findViewById(R.id.tv_reg_priv);
		privTv.setOnClickListener(this);

		checkB = (CheckBox) findViewById(R.id.cb_reg_agree);
		mCount = new TimeCount(60 * 1000, 1000);
	}

	@Override
	public void onClick(View v) {
		userName = mPhone.getText().toString().trim();

		switch (v.getId()) {
		case R.id.send_btn:// 发送验证码
			if (KStringTool.isEmpty(userName)) {
				mPhone.setError("手机号不能为空");
				return;
			}
			Map<String, String> map2 = new TreeMap<String, String>();
			map2.put("phone", userName);
			new SendSMSCodeTask(this, 10020, this).execute(map2);
			mCount.start();
			break;
		case R.id.confirm_btn:// 提交
			userPwd = mRePwd.getText().toString().trim();
			String userCode = mCode.getText().toString().trim();

			if (!checkB.isChecked()) {
				ToastUtil.showShortMsg("未同意服务条款！");
				return;
			}

			if (KStringTool.isNotEmpty(userName) && KStringTool.isNotEmpty(userPwd) && KStringTool.isNotEmpty(userCode)) {
				Map<String, String> map = new TreeMap<String, String>();
				map.put("phone", userName);
				map.put("passwd", userPwd);
				map.put("source", "2");
				map.put("type", "0");
				map.put("code", userCode); // 验证码参数

				new RegTask(this, 10021, this).execute(map);
			}

			break;
		case R.id.tv_reg_priv:// 服务条款
			startActivity(BaseWebViewActivity.createIntent(this, Constant.ConfigNet.priv, "服务条款", false, false, WebViewActivity2.class));
			break;
		default:
			break;
		}
	}

	/**
	 * 倒计时的内部类
	 * 
	 */
	public class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			mSend.setClickable(false);// 设置不可点击
			mSend.setText("倒计时：" + millisUntilFinished / 1000 + "秒");
		}

		@Override
		public void onFinish() {
			mSend.setClickable(true);// 倒数一秒的时候 获得点击
			mSend.setText("获取验证码");
		}

	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {
		if (object instanceof NetworkException) {
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
			return;
		}
		if (taskId == 10020) {

		} else {
			Toast.makeText(this, "该手机号码已经注册，请登录", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		if (taskId == 10020) {
			Toast.makeText(this, "验证码获取成功", Toast.LENGTH_SHORT).show();
		} else if (taskId == 10021) {
			UserManger.getInstance().recordUserName(this, userName);// 只需记住用户名
			// String userId = object.toString();
//			 ServiceAuthManager.saveAuthParamsUserId(this, userId);//注册时由登录过来的
			// 注册成功后必须重新登录一遍 此参数在登录成功后保存
			Toast.makeText(this, "注册成功,请重新登录", Toast.LENGTH_SHORT).show();
			finish();
		}
	}

	// Um
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(this.getClass().getSimpleName());
		MobclickAgent.onResume(this); // 统计时长
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(this.getClass().getSimpleName());
		MobclickAgent.onPause(this);
	}

}
